<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$this->plugin_setting('setting');
$id=$_RQ['id'];
$return['params']=array();
if($_RQ['op']=='main'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('该门店不存在！','error');
	}
	$_SESSION['store_id']=$id;
	$goodsid=intval($_RQ['goodsid']);
	$return['fromgoods']=array();
	if($goodsid){
		$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
		$return['fromgoods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),$fields);
		$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
		$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
		$return['fromgoods']['cartid']=$cartinfo['id']?:0;
		$return['fromgoods']['carttotal']=$cartinfo['total']?:0;
		$return['fromgoods']['cartprice']=$cartinfo['price']?:0;
	}
	$store=pdo_get('jy_weishop_store',array('id'=>$id));
	pdo_update('jy_weishop_store',array('looks +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));		
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$store['out_price']=$store['out_price']>0?$store['out_price']:$citypeisong['out_price'];
	$store['goodscount']=pdo_count('jy_weishop_goods',array('merchid'=>0));
	$store['ordercount']=pdo_count('jy_weishop_order',array('storeid'=>$id));
	$store['salenum'] +=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$id),'total');
	$store['lat_lng']=json_decode($store['lat_lng'],true);
	$store['photo_list'] = json_decode($store['photo_list'],true);
	$store['businessimg'] = json_decode($store['businessimg'],true);
	$store['licenseimg'] = json_decode($store['licenseimg'],true);
	$store['onlinetime']=$store['onlinetime']?json_decode($store['onlinetime'],true):array('star'=>'08:00','end'=>'22:00');
	$store['onlinetime']['star']=$store['onlinetime']['star']?:'08:00';
	$store['onlinetime']['end']=$store['onlinetime']['end']?:'22:00';	
	$goodsids=pdo_getall('jy_weishop_goods',array('merchid'=>0),'id','id');
	if($goodsids){
		$goodsids=array_keys($goodsids);
		$store['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsids),'tags');
	}
	$tags=str_array($return['pluginset']['tags'],'|');
	$key=mt_rand(0,count($tags));
	$store['tags']=$store['tags']?:$tags[$key];
	$store['coupons']=model('jy_weishop_en/coupon')->getmerchcoupon('',1);
	$return['store']=$store;
	$return['kefu']=$store['iskefu'];
	$return['setting']=$setting;
	$return['params']=array('page'=>1,'merchid'=>0);
	//凭排号下单
	$tableid = intval($_RQ['tableid']);
	if($tableid){
		$this->session('tableid',$tableid);
	}
	if($tableid){
		$table = pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
		//不是我在用
		if($table['nowuserid'] !=$_SESSION['uid']){
			if($table['status'] <1){
				$this->session('tableid','');
				exi('不好意思，该餐桌未开放，请选择其他餐桌！','error','jy_weishop_en/index/');
			}
			if($table['busy']>0 && empty($store['in_add'])){
				$this->session('tableid','');
				exi('餐桌已被占用，请重新选择餐桌!','error','jy_weishop_en/index/');
			}
		}
		pdo_update('jy_weishop_table',array('scanningnum +'=>1,'busy'=>1,'nowuserid'=>$_SESSION['uid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
	}
	//补充
	$category=pdo_getall('jy_weishop_goods_group',array('mid'=>$store['mid'],'uniacid'=>$_SESSION['uniacid'],'parentid'=>0));
	if(empty($category)){
		$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0));
	}
	$getcarts=model('jy_weishop_en/cart')->getall();
	$carts=$getcarts['list'];
	$allnum=$getcarts['alltotal'];
	$goodsprice=$getcarts['goodsprice'];
	foreach($category as $k=>&$v){
		$allgoods=pdo_getall('jy_weishop_goods',array('merchid'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>$v['id']),'id');
		$v['total']=0;
		foreach($allgoods as $ag){
			$carttotal=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$v['total'] +=$carttotal;
		}
		if(empty($allgoods)){
			unset($category[$k]);
		}
	}
	$othergoods=pdo_getall('jy_weishop_goods',array('merchid'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>0),'id');
	$total=0;
	foreach($othergoods as $ag){
			$carttotal=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$total +=$carttotal;
	}
	$category[]=array('id'=>'0','title'=>'其他分类','total'=>$total);
	$category=array_values($category);
	//运费计算：
	$order=array(
		'list'=>$carts,
		'total_price'=>$goodsprice,
	);
	$up_dispatch=model('jy_weishop_en/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id']);
	$return['dispatchprice']=$up_dispatch['get_freight']['fee'];
	$allprice = $goodsprice - $youhui;
	$return['category']=$category;
	$return['nowckey']=0;
	$return['params']['pcate']=$category[0]['id'];
	$return['allnum']=$allnum;
	$return['totalprice']=$allprice;
	$return['carts']=$carts;
	$return['showlist']='goods';
	$sharelink='jy_weishop_en/store.goodslist//id='.$id.'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$store['title'],
			'desc'=>$store['desc']?$store['desc']:'门店描述，一个属于自己的门店！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($store['logo'])
	);
	set_html('title',$store['title']);
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'nochecked'=>0,'no_showsys'=>0,'merchid'=>0);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}else{
		if($_RQ['pcate']){
			$params['pcate']=intval($_RQ['pcate']);
			$return['params']['pcate']=intval($_RQ['pcate']);
		}
		if($_RQ['ccate']){
			$params['ccate']=intval($_RQ['ccate']);
			$return['params']['ccate']=intval($_RQ['ccate']);
		}
	}
	$storeset=$this->plugin_setting('storeset');
	if($storeset['no_goods']!=1){
		$goodsids=pdo_getall('jy_weishop_store_goods',array('storeid'=>$_SESSION['store_id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']),'goodsid','goodsid');
		if($goodsids){
			$goodsids=array_keys($goodsids);
			$params['id']=$goodsids;
		}
	}
		//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 6;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
	$orderby='salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop_en/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}
	if($list){
		foreach($list as &$itemv){
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				//已选规格
				$selectspecs = array();
				$selespename = '';
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							$selectspecs[$k]=$sp['items'][0]['id'];
							$selespename .=$sp['items'][0]['title'].'，';
						}
				}
				//默认已选参数：
				$spsids = '';
				foreach($selectspecs as $k=>&$s){
						if($k==0){
							$spsids .= $s;
						}else{
							$spsids .= '_'.$s;
						}
				}
				if($store_id){
					$getinfo=model('jy_weishop_en/store')->getinfo($itemv['id'],$store_id);
					$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
					$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
					$itemv['options']=model('jy_weishop_en/store')->getoption($itemv['id'],$store_id,$spsids);
				}
				if(!$itemv['options']['id']){
					$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
				}
				//默认商品规格价格：
				if($itemv['options']){
					$itemv['price']=$itemv['options']['price'];
					if($itemv['active']['discount']>10){
						$itemv['options']['oldprice']=$itemv['options']['price'];
						$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
						$itemv['options']['price']=$itemv['price'];
					}
					$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
					$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
				}
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$itemv['optionid']=$itemv['options']['id'];
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
		}
	}
	exi($list);
}
?>